﻿// ******************************************************************
//        /\ /|       @file       Feishu2NdLevelApprovers.cs
//        \ V/        @brief
//        | "")       @author     向征, xiangzheng@shjinjia.com.cn
//        /  |
//       /  \\        @Modified   2023-11-10
//     *(__\_\        @Copyright  Copyright (c) 2023, 向征
// ******************************************************************
// 

using System.Collections.Generic;
using System.Threading.Tasks;
using Jinjia.Approve.IRepository;
using Jinjia.Approve.IService.JJFlow.Approvers;
using Jinjia.Approve.Model.VM.Org;

namespace Jinjia.Approve.Service.JJFlow.Approvers
{
    /// <summary>
    /// 获取飞书二级领导审批、二级领导审批
    /// </summary>
    public class Feishu2NdLevelApprovers : IJJApprovers
    {
        private readonly IErpaUserRepository _erpaUserRepository;

        public Feishu2NdLevelApprovers(IErpaUserRepository erpaUserRepository)
        {
            _erpaUserRepository = erpaUserRepository;
        }
        
        public async Task<List<int>> GetInstanceNodeEmpInfo(string biz_order_id, int start_user = 0, string depart_id = "0", Dictionary<string, string> dic = null)
        {
            UserParentOrg userParentOrg = _erpaUserRepository.GetUserParentOrg(start_user, depart_id);

            int count = 2;
            // 此人是部门领导再往上找一级
            count = userParentOrg.LeaderUserId == start_user ? count + 1 : count;
            
            for (int i = 0; i < count; i++)
            {
                userParentOrg = _erpaUserRepository.GetUserParentOrg(start_user, depart_id);
                start_user = default;
                depart_id = userParentOrg.ParentDepartmentId;
            }

            var result = new List<int> { userParentOrg.LeaderUserId };

            return await Task.FromResult(result);
        }
    }
}